package com.diaohw.platform.common.obj.thread;

import com.diaohw.platform.common.obj.login.LoginUser;

public class LoginThread {

    private boolean login;

    private LoginUser user;


    private final static ThreadLocal<LoginThread> threadLocal = new InheritableThreadLocal<>();

    public static void init(LoginUser user) {
        LoginThread thread = new LoginThread();
        thread.login = true;
        thread.user = user;
        threadLocal.set(thread);
    }

    public static LoginThread get() {
        return threadLocal.get();
    }

    public static LoginUser clear() {
        LoginUser user = getUser();
        threadLocal.remove();
        return user;
    }

    public static LoginUser getUser() {
        return get() != null ? get().user : null;
    }

    public static boolean isLogin() {
        return get() != null && get().login;
    }

    public static String getLoginId() {
        return get() != null ? get().user.getLoginId() : null;
    }

    public static String getUsername() {
        return get() != null ? get().user.getUsername() : null;
    }

    public static String getNickname() {
        return get() != null ? get().user.getNickname() : null;
    }

}
